package com.yodawnla.easyPuyo.HUD;

import org.anddev.andengine.entity.IEntity;
import org.anddev.andengine.entity.modifier.AlphaModifier;
import org.anddev.andengine.entity.modifier.IEntityModifier.IEntityModifierListener;
import org.anddev.andengine.entity.modifier.ScaleModifier;
import org.anddev.andengine.entity.modifier.SequenceEntityModifier;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.util.modifier.IModifier;
import org.anddev.andengine.util.modifier.ease.EaseStrongIn;

import com.yodawnla.easyPuyo.R;
import com.yodawnla.easyPuyo.system.Fonts;
import com.yodawnla.easyPuyo.system.Tex;
import com.yodawnla.easyPuyo.system.Values;
import com.yodawnla.lib.YoActivity;
import com.yodawnla.lib.util.widget.YoButton;
import com.yodawnla.lib.util.widget.YoText;

public class AreaClearEffect extends Hud
{
	public static AreaClearEffect getInstance()
	{
		if (instance == null)
			instance = new AreaClearEffect();
		return instance;
	}
	static AreaClearEffect instance;
	
	Sprite mAreaClearSprite;
//	YoEmitter mChestEffect;
	YoButton mBg;
	
	YoText mRewardText;
	
	private AreaClearEffect()
	{
		super(HudValue.Name.AreaClear, HudValue.ZIndex.AreaClear);
		
		mBg = new YoButton(mHud, -100, -100, 600,1000, YoActivity.getBaseActivity().getTexture(Tex.White))
		{
//			public void onClickedEvent()
//			{
//				hide();
//				mChestEffect.pause();
//			}
		};
		mLayer.attachChild(mBg);
		mBg.setColor(0, 0, 0);
		mBg.setAlpha(0.7f);
		
		mAreaClearSprite = new Sprite(0, 230, YoActivity.getBaseActivity().getTexture(Tex.AreaClear));
		mLayer.attachChild(mAreaClearSprite);
		mAreaClearSprite.setPosition(240 - mAreaClearSprite.getWidth()/2, mAreaClearSprite.getY());
		
		mRewardText = new YoText(0, mAreaClearSprite.getY() + mAreaClearSprite.getHeight() + 10, Fonts.White40, "+"+Values.StageClearDiamond+" "+YoActivity.getBaseActivity().getRString(R.string.Diamond));
		mRewardText.setPosition(240 - mRewardText.getWidth()/2, mRewardText.getY());
		mLayer.attachChild(mRewardText);
		
		
//		YoEmitter.Param param = new YoEmitter.Param(YoActivity.getBaseActivity().getTexture(Tex.Spark), 1000, 1000, 50);
//		param.velocityInit(0, 0, -200, -400);
////		param.mX = mChest.getWidth() / 2 - 20;
////		param.mY = mChest.getHeight() / 2;
//		param.mX = 240;
//		param.mY = 600;
//		param.scaleInit(1.5f, 3.5f);
//		param.mType = YoEmitter.EmitterType.RECTANGLE;
//		param.mWidth = 480;
//		param.mHeight = 400;
//		param.mMinLifeTime = 0.5f;
//		param.mMaxLifeTime = 1.6f;
//		param.mSourceBlendFunction = GL10.GL_ONE;
//		param.mDestinationBlendFunction = GL10.GL_ONE_MINUS_SRC_COLOR;
//
//		mChestEffect = new YoEmitter(param);
//		mChestEffect.attachTo(mLayer);
//		mChestEffect.pause();
	}
	
	public void show()
	{
		mBg.setAlpha(0.7f);
		mRewardText.setAlpha(1);
		mRewardText.setVisible(false);
		
		mAreaClearSprite.setAlpha(1);
		mAreaClearSprite.setVisible(false);
		super.show();
		
		mAreaClearSprite.registerEntityModifier(
				new SequenceEntityModifier(
						new ScaleModifier(0.5f, 0, 1, new IEntityModifierListener()
						{
							
							public void onModifierStarted(IModifier<IEntity> pModifier, IEntity pItem)
							{
								mAreaClearSprite.setVisible(true);
								
							}
							
							public void onModifierFinished(IModifier<IEntity> pModifier, IEntity pItem)
							{
								mRewardText.setVisible(true);
								mRewardText.registerEntityModifier(new ScaleModifier(0.25f, 0, 1));
								
//								mChestEffect.resume();
//								YoTimer timer = new YoTimer(0.2f)
//								{
//									public void onTimePassed()
//									{
//										mChestEffect.pause();
//										stop();
//									}
//								};
//								timer.start();
							}
						},EaseStrongIn.getInstance()),
						new AlphaModifier(1.5f, 1, 1),
						new AlphaModifier(0.4f, 1, 0, new IEntityModifierListener()
						{
							
							public void onModifierStarted(IModifier<IEntity> pModifier, IEntity pItem)
							{
								mBg.registerEntityModifier(new AlphaModifier(0.4f, mBg.getAlpha(), 0));
								mRewardText.registerEntityModifier(new AlphaModifier(0.4f, mBg.getAlpha(), 0));
							}
							
							public void onModifierFinished(IModifier<IEntity> pModifier, IEntity pItem)
							{
								
								hide();
							}
						})
					));
	}
}
